package com.cardbaobao.cardbabyclient.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.utils.SoftKeyboardUtils;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;

/**
 * Created by Jiangzx on 2017/5/4.
 * 输入验证码Dialog，验证码为短信验证码，只需要提供一个输入框即可
 */

public class AuthCodeCharacterDialog extends Dialog {
    private EditText etCode;
    public AuthCodeCharacterDialog(@NonNull final Context context) {
        super(context, R.style.dialog);
        View view= LayoutInflater.from(context).inflate(R.layout.dialog_auth_code_character,null);
        etCode= (EditText) view.findViewById(R.id.et_auth_code);
        view.findViewById(R.id.bt_commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code=etCode.getText().toString().trim();
                if(TextUtils.isEmpty(code)){
                    ToastUtil.showToast("验证码不能为空");
                }else{
                    if(listener!=null){
                        listener.onCommitButtonClicked(etCode.getText().toString().trim());
                    }
                }
            }
        });
        view.findViewById(R.id.ibt_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setContentView(view);
        setCanceledOnTouchOutside(false);//dialog外部点击不会消失
        setCancelable(false);//按下返回键不会消失
        Window window=getWindow();
        if(window!=null){
            WindowManager.LayoutParams params=window.getAttributes();
            params.width= (int) (ScreenUtil.getScreenWidth(context)*0.9f);
            window.setAttributes(params);
        }
    }

    public void clearText(){
        if(etCode!=null){
            etCode.setText("");
        }
    }

    private OnAuthCodeButtonClicked listener;

    public void setButtonClickedListener(OnAuthCodeButtonClicked listener){
        this.listener=listener;
    }

    public interface OnAuthCodeButtonClicked{
        void onCommitButtonClicked(String authCode);
    }

    public void callSoftKeyBoard(){
        SoftKeyboardUtils.showSoftKeyboard(etCode);
    }
}
